/**

 @Name：响应参数解析
 @Date: 2018-08
    
 */

 layui.define(["layer"], function (exports) 
 {
	 var layer = layui.layer;
   var $ = layui.$;
	 var loginPage = "../../views/user/login_by_username_new.html";
	 
	 var obj = 
	 {
			 // 服务器地址：
			 getReqstUrl: function(key) 
			 {
					/*
						传入key值：
						空							-1
						Usersmanage		 0:用户管理
						Products			 1:产品管理 :10012
					*/
					
					 var rootpath = "http://10.141.31.239/";				// Server
					// var rootpath = "http://10.139.7.55:8080/";			// XMH
					// var rootpath = "http://10.139.59.116:8080/"; 	// HYP							
					// var rootpath = "http://10.139.23.100:8080/";		// ZFC - 用户
					// var rootpath = "http://10.139.24.16:9090/";		// PJH		
					
					var subpath = this.getValueForKey("contextPath", key);
					var path = rootpath.concat(subpath);
					return path;
			 },
			 
			 // 页面间跳转传参 -> 获取用户ID;
			 // xxx.html?userid=xxx
			 getParamValueForKey: function(key)
			 {
					var dic = {};
					var urlInfo = window.location.href;  
					var argsIndex = urlInfo.indexOf("?");  
					var args = urlInfo.substring(argsIndex+1).split("&"); 
					//console.log("前个页面地址:"+urlInfo);
					//console.log("分离页面参数:"+args);
					//console.log("共有"+(args.length)+"个参数：");

					if(args.length>0)
					{
						for(i=0;i<args.length;i++)
						{  
							var argResult = args[i].split("=");
							dic[argResult[0]] = argResult[1];
							//console.log("参数"+i+"键为: "+argResult[0]+"; 值为: "+argResult[1])
						}
						return dic[key];
					}
			 },
			 
			 // 请求传入的【参数检查】
			 checkInput: function(value)
			 {
					var temp = String(value);
					if(temp.indexOf("undefined")>-1 || value==null)
						return "";
					else
						return value;
			 },
			 
			 
			 //【jQuery】从cookie中取出token; 注意return({对象})格式!
			 getLocalCookies: function(key)
			 {
					var strCookie = document.cookie;
					var arrCookie = strCookie.split(";");
					var value;
					for(var i=0;i<arrCookie.length;i++)
					{
							//console.log("cookie对儿:"+arrCookie[i]);
							var pair = arrCookie[i];
							var index = pair.indexOf(key);
							if(index!=-1)
							{
									var parts = pair.split("=");
											value = parts[1];
							}
					}
					return value;
			 },
			 
			 
			 // 数据返回【错误处理】
			 errCodeLogic: function(code, msg)
			 {
				console.log("Err Code:"+code+"Err Msg:"+msg);
			 	switch(code)
			 	{
			 		case 600602:
			 		{
						layer.msg("您当前尚未登陆,系统将为您跳转...");
						setTimeout(
							function()
							{ 
								 location.replace(loginPage); 
							}, 2000);
						
			 		};break;
			 		case 600603:
			 		{
						layer.msg("登陆超时,系统将为您跳转...");
						setTimeout(
							function()
							{ 
								 location.replace(loginPage); 
							}, 2000);
					};break;
			 		default:
			 		{
			 			layer.msg("出错啦:" + msg);
			 		}
			 	}
			 },
			 
			 
			 // 数据返回【处理】
			 parseResps: function(res)
			 {
				  // string -> object
					// var obj = JSON.parse(res); 
					// object -> string
					// var obj = JSON.stringify(res);
					console.log("结果数据："+JSON.stringify(res.data)); 
			 	
					if(res.code!=0)
					{
						this.errCodeLogic(res.code, res.msg);
						res.data = null;
					}
					return res;
			 },
			 
			 
			 // 本地【cookie清空】
			 clearAllCookies: function() 
			 {
					var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
					if(keys) 
					{
						for(var i = keys.length; i--;)
							document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
					}
			 },
			 
			 
			 //【毫秒】转为【yyyy-MM-dd 00:00:00】
			 getDate:function(millisec)
			 {
          var date = new Date(millisec);
              datetimeType = date.getFullYear() +"-"+
														 getMonth(date) +"-"+ 
														 getDay(date)+" "+
														 getHours(date)+":"+
														 getMinutes(date)+":"+
														 getSeconds(date);
					
          return datetimeType;

          //返回 01-12 的月份值
          function getMonth(date){
              var month = "";
                  month = date.getMonth() + 1; //getMonth()得到的月份是0-11
              if(month<10){
                  month = "0" + month;
              }
              return month;
          }
					
					//返回01-30的日期
          function getDay(date){
              var day = "";
                  day = date.getDate();
              if(day<10){
                  day = "0" + day;
              }
              return day;
          }
					//返回小时
          function getHours(date){
              var hours = "";
                  hours = date.getHours();
              if(hours<10){
                  hours = "0" + hours;
              }
              return hours;
          }
					//返回分
          function getMinutes(date){
              var minute = "";
              minute = date.getMinutes();
              if(minute<10){
                  minute = "0" + minute;
              }
              return minute;
          }
					//返回秒
          function getSeconds(date){
              var second = "";
                  second = date.getSeconds();
              if(second<10){
                  second = "0" + second;
              }
              return second;
          }
       },
			 
			 
			 // 根据值, 返回键(一一对应)
			 getKeyForValue: function(dic, value, compare = (a, b) => a === b) 
			 {
					return Object.keys(dic).find(k => compare(data[k], value))
			 },
			 
			 
			 // 根据键, 获取值(一一对应)
			 getValueForKey: function(subdic,key)
			 {
				 var dic = 
				 {
						contextPath:
						{
							"-1": "",
							"0": "Usersmanage/",
							"1": "Products/"
						},
						
						usrOpType:
						{
							"0": "修改邮箱",
							"1": "修改电话",
							"2": "修改银行卡",
							"3": "修改密码"
						},
						
						usrOpSrc:
						{
							"0": "用户管理平台",
							"1": "产品管理平台",
							"2": "投资平台",
							"3": "验证平台",
							"4": "兑换平台",
							"5": "短租平台",
							"6": "微信平台",
							"7": "微信兑换平台"
						},
						
						userRoleType:
						{
							"normuser": "100",
							"root": "101",
							"label": "102",
							"rule": "103",
							"check": "104",
							"input": "105",
							"role": "106",
							"rolecheck": "107",
							"goodsroot": "108"
						},
						
						userRoleStr:
						{
							"normuser": "普通注册账号",
							"root": "超级管理员",
							"label": "标签设置员",
							"rule": "规则设置员",
							"check": "审核员",
							"input": "录入员",
							"role": "角色设置员",
							"rolecheck": "角色审核员",
							"goodsroot": "商品管理员"
						},
						
						adminOpType:
						{
							"1011": "修改用户角色",
							"1012": "修改用户状态"
						}
				 };
				 
				 return dic[subdic][key];
			 }
   };
	 
   exports("response",obj);
});
